﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;

using Wicresoft.Shell.Core;

namespace Wicresoft.Shell.Package.AppExp
{
    public partial class AddConnectForm : Form
    {
        #region Fields

        private Boolean m_ServerNameIsSet;
        private Boolean m_UserNameIsSet;
        private Boolean m_PasswordIsSet;
        private Boolean m_DBNameIsSet;

        private Boolean m_ConnectSuccessful;
        private Boolean m_IsNewConnect;

        #endregion

        #region Constructor

        public AddConnectForm()
        {
            InitializeComponent();

            ResetControlsState();
        }

        #endregion

        #region PrivateMethods

        private void ResetControlsState()
        {
            if (m_IsNewConnect)
            {
                m_ServerNameIsSet = false;
                m_UserNameIsSet = false;
                m_PasswordIsSet = false;
                m_DBNameIsSet = false;

                txtServerName.Clear();
                txtUserName.Clear();
                txtPassword.Clear();
                ckbSavePassword.Checked = false;
                cboDBName.Text = String.Empty;
                cboDBName.Items.Clear();
            }
            else
            {
                txtPassword.Clear();
                ckbSavePassword.Checked = false;
                m_ServerNameIsSet = (txtServerName.TextLength > 0);
                m_UserNameIsSet = (txtUserName.TextLength > 0);
                m_PasswordIsSet = false;
                m_DBNameIsSet = (cboDBName.Text.Length > 0);
            }

            SetControlsEnablede();
        }

        private void SetControlsEnablede()
        {
            if (m_IsNewConnect)
            {
                txtServerName.Enabled = true;
                txtUserName.Enabled = true;
                cboDBName.Enabled = (m_ServerNameIsSet && m_UserNameIsSet);
                btnTestConnect.Enabled = (m_ServerNameIsSet && m_UserNameIsSet);
                btnOk.Enabled = (m_ServerNameIsSet && m_UserNameIsSet && m_DBNameIsSet);
            }
            else
            {
                txtServerName.Enabled = !m_ServerNameIsSet;
                cboDBName.Enabled = !m_DBNameIsSet;
                btnTestConnect.Enabled = (m_ServerNameIsSet && m_UserNameIsSet);
                btnOk.Enabled = (m_ServerNameIsSet && m_UserNameIsSet && m_DBNameIsSet);
            }
        }

        private void btnTestConnect_Click(Object sender, EventArgs e)
        {
            if (cboDBName.Text.Length == 0)
            {
                FillDBNamesList(true);
            }
            else
            {
                TestDBConnection();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            String errInfo = String.Empty;
            
            if (!m_ConnectSuccessful)
                m_ConnectSuccessful = MySqlHelper.TestDBConnection(ServerName, DBName, UserName, Password, out errInfo);

            if (m_ConnectSuccessful)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show(errInfo, Resources.MsgBox_Failed, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void txtServerName_TextChanged(object sender, EventArgs e)
        {
            m_ServerNameIsSet = (txtServerName.TextLength > 0);
            m_ConnectSuccessful = false;
            cboDBName.Text = String.Empty;
            cboDBName.Items.Clear();
            SetControlsEnablede();
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            m_UserNameIsSet = (txtUserName.TextLength > 0);
            m_ConnectSuccessful = false;
            SetControlsEnablede();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            m_PasswordIsSet = (txtPassword.TextLength > 0);
            m_ConnectSuccessful = false;
            SetControlsEnablede();
        }

        private void cboDBName_TextChanged(object sender, EventArgs e)
        {
            m_DBNameIsSet = (cboDBName.Text.Length > 0);
            m_ConnectSuccessful = false;
            SetControlsEnablede();
        }

        private void cboDBName_DropDown(object sender, EventArgs e)
        {
            if (cboDBName.Items.Count == 0)
            {
                FillDBNamesList(false);
            }
        }

        private void AddConnectForm_Shown(object sender, EventArgs e)
        {
            m_ConnectSuccessful = false;
            ResetControlsState();
        }

        private void FillDBNamesList(Boolean showResutlInfo)
        {
            String[] dbNames = null;
            String errInfo = String.Empty;

            cboDBName.Items.Clear();

            if (GetDBNames(out dbNames, out errInfo))
            {
                Array.Sort(dbNames);
                foreach (String eachDBName in dbNames)
                    cboDBName.Items.Add(eachDBName);
                if (showResutlInfo)
                {
                    MessageBox.Show(Resources.AddConnectForm_ConnectSuccessful, Resources.MsgBox_Info,
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show(errInfo, Resources.MsgBox_Failed, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void TestDBConnection()
        {
            String errInfo = String.Empty;

            if (MySqlHelper.TestDBConnection(ServerName, DBName, UserName, Password, out errInfo))
            {
                m_ConnectSuccessful = true;
                MessageBox.Show(Resources.AddConnectForm_ConnectSuccessful, Resources.MsgBox_Info,
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                m_ConnectSuccessful = false;
                MessageBox.Show(errInfo, Resources.MsgBox_Failed, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private Boolean GetDBNames(out String[] dbNames, out String errInfo)
        {
            return MySqlHelper.GetDBNames(this.ServerName, this.UserName, this.Password, out dbNames, out errInfo);
        }

        #endregion

        #region Properties

        public String ServerName
        {
            get { return txtServerName.Text; }
            set { txtServerName.Text = value; }
        }

        public String UserName
        {
            get { return txtUserName.Text; }
            set { txtUserName.Text = value; }
        }

        public String DBName
        {
            get { return cboDBName.Text; }
            set { cboDBName.Text = value; }
        }

        public String Password
        {
            get { return txtPassword.Text; }
        }

        public Boolean IsSavedPassword
        {
            get { return ckbSavePassword.Checked; }
        }

        public Boolean IsNewConnect
        {
            get { return m_IsNewConnect; }
            set { m_IsNewConnect = value; }
        }

        #endregion
    }
}
